/*
 * Anything: A flexible and easy to use data structure that combines the 
 * characteristics of a list and a hashmap into one data structure.
 * 
 * Based on a paper by Peter Sommerlad and Marcel Rueedi on 
 * 'Do-it-yourself reflection' entered at EuroPLOP 1998.
 * 
 * This library was written completely from scratch and is based only 
 * in spirit on the original Java Anything implementation written by 
 * Andre Weinand/Marcel Rueedi in 1997/99 and enhanced by Stefan Tramm 
 * in 2006.
 * 
 * Copyright (c) 2008 Thomas Marti and others. All rights reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package any.functor.impl.pred;

import java.util.Map.Entry;

import any.Any;
import any.ImmutableAny;
import any.functor.interfaces.ExactNumberPredicateBuilder;
import any.functor.interfaces.Predicate;
import any.functor.interfaces.PredicateAndInclusiveExclusiveBuilder;

import static any.functor.Functions.asIterable;
import static any.functor.Predicates.canBeNumber;

public class NumberAnyPredicates {
	
	public static ExactNumberPredicateBuilder isExact() {
		return new ExactNumberPredicateBuilder();
	}
	
	// ----------------------------------------------------------------------------------------
	
	public static final Predicate<Entry<String, ? extends ImmutableAny>> greaterThan(Number value) throws IllegalArgumentException {
		return new BaseComparingNumberPredicate(value) {
			@Override protected boolean compared(int compared) {
				return compared > 0;
			}
			@Override public String toString() {
				return "greaterThan " + valueBD;
			}
		};
	}

	public static final Predicate<Entry<String, ? extends ImmutableAny>> greaterOrEqualThan(Number value) throws IllegalArgumentException {
		return new BaseComparingNumberPredicate(value) {
			@Override protected boolean compared(int compared) {
				return compared >= 0;
			}
			@Override public String toString() {
				return "greaterOrEqualThan " + valueBD;
			}
		};
	}
	
	public static final Predicate<Entry<String, ? extends ImmutableAny>> smallerThan(Number value) throws IllegalArgumentException {
		return new BaseComparingNumberPredicate(value) {
			@Override protected boolean compared(int compared) {
				return compared < 0;
			}
			@Override
			public String toString() {
				return "smallerThan " + valueBD;
			}
		};
	}
	
	public static final Predicate<Entry<String, ? extends ImmutableAny>> smallerOrEqualThan(Number value) throws IllegalArgumentException {
		return new BaseComparingNumberPredicate(value) {
			@Override protected boolean compared(int compared) {
				return compared <= 0;
			}
			@Override
			public String toString() {
				return "smallerOrEqualThan " + valueBD;
			}
		};
	}
	
	public static final PredicateAndInclusiveExclusiveBuilder<Entry<String, ? extends ImmutableAny>, Number> isBetween(final Number value1) throws IllegalArgumentException {
		return new PredicateAndInclusiveExclusiveBuilderImplementation(value1);
	}
	
	// ----------------------------------------------------------------------------------------

	public static void main(String[] args) {
		Predicate<Entry<String, ? extends ImmutableAny>> numberBetween2And3 = canBeNumber().and(isBetween(2).exclusive().and(3).inclusive());
		System.out.println(numberBetween2And3);

		ImmutableAny numbers = Any.create.any().appendEach(1, 2, 2f, 2.2, "2.000000000000000001", "a", 3, 2.9999, 4, 2.5, -3);
		System.out.println(numbers.give(asIterable().select(numberBetween2And3)));
		System.out.println(numbers.give(asIterable().select(canBeNumber().and(isBetween(2).exclusive().and(3)))));
	}

}
